package org.csu.mall.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Shipping;
import org.csu.mall.entity.Shop;
import org.csu.mall.persistence.ShippingMapper;
import org.csu.mall.service.IShippingService;
import org.csu.mall.utils.UsernameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShippingServiceImpl implements IShippingService {

    @Autowired
    private ShippingMapper shippingMapper;

    @Override
    public UniformResponse<Shipping> addShipping(Shipping shipping) {
        String username= UsernameUtil.getUsername("username");
        Integer userid= Integer.valueOf(UsernameUtil.getUserId("userid"));
        if(username==null){
            return UniformResponse.notLogining();
        }else{
            shipping.setUserId(userid);
            shipping.setCreateTime(LocalDateTime.now());
            shipping.setUpdateTime(LocalDateTime.now());
            int rows = shippingMapper.insert(shipping);
            if(rows == 0){
                    return UniformResponse.createForErrorMessage("收获地址添加失败");
                }
            return UniformResponse.createForSuccess("收获地址添加成功",shipping);
        }
    }

    @Override
    public UniformResponse<String> deleteShipping(String id) {
        String username= UsernameUtil.getUsername("username");
        String userid= UsernameUtil.getUserId("userid");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            Map<String, Object> columnMap = new HashMap<String, Object>();
            columnMap.put("user_id", userid);
            columnMap.put("id", id);
            int rows=shippingMapper.deleteByMap(columnMap);
            if(rows==0){
                return UniformResponse.createForErrorMessage("收获地址删除失败");
            }
            return UniformResponse.createForSuccessMessage("收获地址删除成功");
        }
    }

    @Override
    public UniformResponse<Shipping> updateShipping(Shipping shipping) {
        String username= UsernameUtil.getUsername("username");
        Integer userid= Integer.valueOf(UsernameUtil.getUserId("userid"));
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            shipping.setUserId(userid);
            shipping.setUpdateTime(LocalDateTime.now());
            int rows=shippingMapper.updateById(shipping);
            if(rows==0){
                return UniformResponse.createForSuccessMessage("收获地址更新失败");
            }
            return UniformResponse.createForSuccess("收获地址更新成功",shipping);
        }
    }

    @Override
    public UniformResponse<Shipping> getShippingInfoOne(String id) {
        String username= UsernameUtil.getUsername("username");
        String userid= UsernameUtil.getUserId("userid");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            Shipping shipping = shippingMapper.selectOne(
                    Wrappers.<Shipping>query().eq("user_id",userid).eq("id",id));
            if(shipping == null){
                return UniformResponse.createForErrorMessage("收货地址不存在");
            }
            return UniformResponse.createForSuccess("收获地址查询成功",shipping);
        }
    }

    @Override
    public UniformResponse<List<Shipping>> getShippingInfo() {
        String username= UsernameUtil.getUsername("username");
        String userid= UsernameUtil.getUserId("userid");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            List<Shipping> shippingList= shippingMapper.selectList(Wrappers.<Shipping>query().eq("user_id",userid));
            if(shippingList==null){
                return UniformResponse.createForErrorMessage("用户还没有添加收货地址") ;
            }
            return UniformResponse.createForSuccess("查询成功",shippingList);
        }
    }
}
